package com.party.course.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.date.DateUtil;
import com.party.common.core.domain.entity.SysUser;
import com.party.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import com.party.course.mapper.UserUniteInfoMapper;
import com.party.course.domain.UserUniteInfo;
import com.party.course.service.IUserUniteInfoService;

import cn.hutool.core.util.IdUtil;

import javax.annotation.Resource;

/**
 * 用户关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class UserUniteInfoServiceImpl extends ServiceImpl<UserUniteInfoMapper, UserUniteInfo> implements IUserUniteInfoService {
    @Resource
    private UserUniteInfoMapper userUniteInfoMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询用户关联
     *
     * @param id 用户关联主键
     * @return 用户关联
     */
    @Override
    public UserUniteInfo view(String clubUserId) {
        List<UserUniteInfo> allByClub = userUniteInfoMapper.findAllByClub(clubUserId);
        if (!allByClub.isEmpty()){
            UserUniteInfo uniteInfo = allByClub.get(0);
            uniteInfo.setPayUserIds(userUniteInfoMapper.findListByClub(uniteInfo.getClubUserId()));
            return uniteInfo;
        }
        return null;
    }

    /**
     * 查询用户关联列表
     *
     * @param userUniteInfo 用户关联
     * @return 用户关联
     */
    @Override
    public List<UserUniteInfo> findList(UserUniteInfo userUniteInfo) {
        return userUniteInfoMapper.selectUserUniteInfoList(userUniteInfo);
    }

    /**
     * 新增用户关联
     *
     * @param userUniteInfo 用户关联
     * @return 结果
     */
    @Override
    public void add(UserUniteInfo userUniteInfo) {
        if (!userUniteInfo.getPayUserIds().isEmpty()) {
            addPays(userUniteInfo);
        }
    }

    /**
     * 修改用户关联
     *
     * @param userUniteInfo 用户关联
     * @return 结果
     */
    @Override
    public void updateEntity(UserUniteInfo userUniteInfo) {
        if (!userUniteInfo.getPayUserIds().isEmpty()) {
            userUniteInfoMapper.deleteByClub(userUniteInfo.getClubUserId());
            addPays(userUniteInfo);
        }
    }

    /**
     * 新增超级俱乐部与交付中心关联关系
     *
     * @param payUserIds 交付中心id集合
     * @param isUpdate   0修改 1新增
     */
    public void addPays(UserUniteInfo userUniteInfo) {
        // 遍历payUserIds列表
        for (String payUserId : userUniteInfo.getPayUserIds()) {
            // 根据payUserId查询用户信息
            SysUser user = sysUserMapper.selectUserById(Long.parseLong(payUserId));
            // 如果用户信息不为空
            if (ObjectUtil.isNotEmpty(user)) {
                // 创建UserUniteInfo对象
                UserUniteInfo uniteInfo = new UserUniteInfo();
                uniteInfo.setClubUserId(userUniteInfo.getClubUserId());
                uniteInfo.setClubUserName(userUniteInfo.getClubUserName());
                // 设置payUserId
                uniteInfo.setPayUserId(payUserId);
                // 设置payUserName
                uniteInfo.setPayUserName(user.getUserName());
                // 设置delFlag为0
                uniteInfo.setDelFlag("0");
                uniteInfo.setUpdateTime(DateUtil.date());
                // 设置id为简单UUID
                uniteInfo.setId(IdUtil.simpleUUID());
                // 插入UserUniteInfo
                userUniteInfoMapper.insert(uniteInfo);

            }
        }
    }

    /**
     * 批量删除用户关联
     *
     * @param ids 需要删除的用户关联主键
     * @return 结果
     */
    @Override
    public int deleteIds(List<String> ids) {
        return userUniteInfoMapper.deleteUserUniteInfoByIds(ids);
    }

    /**
     * 删除用户关联信息
     *
     * @param id 用户关联主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return userUniteInfoMapper.deleteUserUniteInfoById(id);
    }
}
